package com.liushao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liushao.entity.Lend;
import com.liushao.entity.LendItem;
import com.liushao.entity.LendItemReturn;
import com.liushao.entity.LendReturn;
import com.liushao.mapper.LendItemMapper;
import com.liushao.mapper.LendItemReturnMapper;
import com.liushao.mapper.LendMapper;
import com.liushao.mapper.LendReturnMapper;
import com.liushao.service.LendItemReturnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liushao.service.UserBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {


    @Autowired
    private LendReturnMapper lendReturnMapper;

    @Autowired
    private LendMapper lendMapper;

    @Autowired
    private LendItemMapper lendItemMapper;

    @Autowired
    private UserBindService userBindService;


    @Override
    public List<LendItemReturn> selectByLendId(Long lendId, Long userId) {
        QueryWrapper<LendItemReturn> wrapper = new QueryWrapper<>();
        wrapper.eq("invest_user_id",userId).
                eq("lend_id",lendId).
                orderByAsc("current_period");

        List<LendItemReturn> lendItemReturnList = baseMapper.selectList(wrapper);

        return lendItemReturnList;
    }

    @Override
    public List<Map<String, Object>> addReturnDetail(Long lendReturnId) {

            //获取还款记录
            LendReturn lendReturn = lendReturnMapper.selectById(lendReturnId);
            //获取标的信息
            Lend lend = lendMapper.selectById(lendReturn.getLendId());

            //根据还款id 获取回款列表
            List<LendItemReturn> lendItemReturnList= this.selectLendItemReturnList(lendReturnId);
            //详细信息
            List<Map<String, Object>> lendItemReturnDetailList= new ArrayList<>();

            for(LendItemReturn lendItemReturn:lendItemReturnList){
                //标的出借记录表
                LendItem lendItem = lendItemMapper.selectById(lendItemReturn.getLendItemId());
                String bindCode = userBindService.getBindCodeByUserId(lendItem.getInvestUserId());
                Map<String, Object> map = new HashMap<>();
            /*
             ：。
   ：
   ：收款人（投资人）
   ： (必须等于base_amt+benifit_amt), 最多支持小数点
                后2位
   ：还款本金。最多小数点后2位
   ：还款利息(如果base_amt、benifit_amt都大于0，则benifit_amt  不能大于base_amt的50%，且该投资单所有还款利息不得超过投资金额的50%),最多小数点后2位。base_amt、benifit_amt至少有一项必须大于0
   fee_amt：商户手续费。最多小数点后2位

             */
                map.put("agentProjectCode",lend.getLendNo());//还款项目编号
                map.put("voteBillNo",lendItem.getLendItemNo());//投资单号
                map.put("toBindCode",bindCode);
                map.put("transitAmt",lendItemReturn.getTotal());//还款金额
                map.put("baseAmt",lendItemReturn.getPrincipal());//还款本金
                map.put("benifitAmt",lendItemReturn.getInterest());//还款利息
                map.put("feeAmt",new BigDecimal("0"));

                lendItemReturnDetailList.add(map);
            }



            return lendItemReturnDetailList;
        }
//  selectLendItemReturnList
        public  List<LendItemReturn>  selectLendItemReturnList(Long lendReturnId){
            QueryWrapper<LendItemReturn> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("lend_return_id",lendReturnId);
            List<LendItemReturn> lendItemReturnList = baseMapper.selectList(queryWrapper);
            return lendItemReturnList;


        }
}
